package com.ldfile.server.utils;

import com.ldfile.server.dto.BaseFileCompanyVO;
import com.ldfile.server.dto.BaseFileDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@Slf4j
@Component
public class ZipUtil {
    private static String locDir;
    private static String fileUrl;
    private static String path;

    public static void zipFile(BaseFileCompanyVO zipInfo) {
        log.debug("开始打包");
        String zipFileName = zipInfo.getZipFileName();
        try {
            File file = new File(locDir + zipFileName);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdir();
            }
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            ZipOutputStream zos = new ZipOutputStream(fileOutputStream);
            for (BaseFileDTO baseFileVO : zipInfo.getBaseFileVOList()) {
                //直接放到压缩包的根目录
                zos.putNextEntry(new ZipEntry(baseFileVO.getNewFileName()));
                FileInputStream fileInputStream = new FileInputStream(baseFileVO.getFilePath().replaceFirst(fileUrl + path, locDir));
                int len = -1;
                byte[] byts = new byte[1024];
                while ((len = fileInputStream.read(byts)) > 0) {
                    zos.write(byts, 0, len);
                }
                fileInputStream.close();
                zos.closeEntry();
                zos.flush();
            }
            zos.close();
        } catch (FileNotFoundException e) {
            log.info("压缩失败");
            e.printStackTrace();
        } catch (IOException e) {
            log.info("压缩失败");
            e.printStackTrace();
        }
        log.debug("打包成功");
    }

    @Value("${file.upload.url}")
    public void setFileUrl(String fileUrl) {
        ZipUtil.fileUrl = fileUrl;
    }

    @Value("${file.upload.dir}")
    public void setLocDir(String locDir) {
        ZipUtil.locDir = locDir;
    }


    @Value("${file.upload.path}")
    public void setPath(String path) {
        ZipUtil.path = path;
    }

    /**
     * 文件打包下载
     */
    public static void downloadZip(List<BaseFileDTO> filePaths, HttpServletResponse response) {
        try {
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-disposition", "attachment;filename=download.zip");
            response.setContentType("application/octet-stream");
            ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
            //添加编号的目的，保证文件名称的唯一，否则出现同名文件夹会报错，导致下载的数据残缺不全
            for (BaseFileDTO baseFileVO : filePaths) {
                //直接放到压缩包的根目录
                zos.putNextEntry(new ZipEntry(baseFileVO.getNewFileName()));
                FileInputStream fileInputStream = new FileInputStream(baseFileVO.getFilePath().replaceFirst(fileUrl + path, locDir));
                int len = -1;
                byte[] byts = new byte[1024];
                while ((len = fileInputStream.read(byts)) > 0) {
                    zos.write(byts, 0, len);
                }
                fileInputStream.close();
                zos.closeEntry();
            }
            zos.close();
        } catch (Exception e) {
            log.error("下载文件异常:" + e.getMessage());
        }
    }

}
